<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .aa{
          width: 300px;
          height: 300px;
          line-height: 300px;
          background: pink;
          color: #fff;
          text-align:center;
          font-size: 32px;
          margin-top: 10px;
        }
    </style>
</head>
<body>
    <div id="pg">
        <com1 @give='fatherEvent'></com1>

        <com2 :bb='a'></com2>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
      <script>
          let com1={
            template:`
                <div> 
                    <button  v-for='(item,index) in title' @click='clickfn(index)' >{{ item.item }}</button>
                </div>
                `,
                data () {
                    return {
                    title: [{
                        item: 'html'
                    },
                    {
                        item: 'css'
                    },
                    {
                        item: 'js'
                    },
                    {
                        item: 'vuejs'
                    },
                ],     
            }
                },
                methods:{
                    clickfn(key){
                    //注册自定义事件
                    // this.$emit("自定义事件名称"，需要发送的数据)
                        this.$emit("give",{
                            num:key
                        })
                    }
                }
          }

          let com2={
              props: ["bb"],
            template:`
                    <div>
                        <div v-for='(item,index) in contents'
                         :class='{aa:pp}'  v-show='index==bb'>{{ item.item }}</div>    
                    </div>
                `,
                data () {
                    return {
                        pp:true,
                    contents: [{
                        item: 'html-contents'
                    },
                    {
                        item: 'css-contents'
                    },
                    {
                        item: 'js-contents'
                    },
                    {
                        item: 'vuejs-contents'
                    }
                ],
                    }
                },
          }

        var pg = new Vue({
              //绑定元素,挂载的页面节点
            el: '#pg',
            // 实例可用数据
            data: {
                pp:true,
                a:0
            },
            methods:{   
           fatherEvent(sondata){
               this.a=sondata.num
            }  
            },
            components: {
                com1,
                com2
             }
        })

      </script>
</body>
</html>